﻿using ApplicationManager.Model;
using DingGC.Library.Cache;
using DingGC.Library.Jobs;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Quartz;
using Quartz.Util;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

namespace ApplicationManager.Services
{
//    [Job("0 0 8,12,18 * * ? ", "内存检测", false)]
//    public class MitmProxyAppJob : IJob
//    {

//        private readonly MitmProxyAppService mitmProxyAppService;
//        private readonly ILogger<MitmProxyAppJob> logger;
//        private readonly IConfiguration configuration;
//        private string key = "mitmproxyapps";
//        public MitmProxyAppJob(MitmProxyAppService mitmProxyAppService, ILogger<MitmProxyAppJob> logger, IConfiguration configuration)
//        {

//            this.mitmProxyAppService = mitmProxyAppService;
//            this.logger = logger;
//            this.configuration = configuration;
//        }

//        public async Task Execute(IJobExecutionContext context)
//        {
//            int maxmemory = configuration.GetValue<int>("MaxMemory");
//            if (maxmemory == 0) maxmemory = 500;
//            logger.LogInformation("MitmProxyAppJob 任务开始......");
//            var appResponse = await mitmProxyAppService.GetAppStatus();
//            foreach (var item in appResponse)
//            {

//                if (item.Status)
//                {
//                    logger.LogInformation($"结束任务{item.Name}");
//                    await mitmProxyAppService.Stop(item.Id);
//                    Thread.Sleep(1000);
//                    await mitmProxyAppService.Start(item.Id);
//                }
//            }


//            logger.LogInformation("MitmProxyAppJob 任务结束");
//        }





//    }



    [Job("0 0/5 * * * ? ", "内存检测2", true)]
    public class MitmProxyApp2Job : IJob
    {

        private readonly MitmProxyAppService mitmProxyAppService;
        private readonly ILogger<MitmProxyApp2Job> logger;
        private readonly IConfiguration configuration;
        private string key = "mitmproxyapps";
        public MitmProxyApp2Job(MitmProxyAppService mitmProxyAppService, ILogger<MitmProxyApp2Job> logger, IConfiguration configuration)
        {

            this.mitmProxyAppService = mitmProxyAppService;
            this.logger = logger;
            this.configuration = configuration;
        }

        public async Task Execute(IJobExecutionContext context)
        {
            int maxmemory = configuration.GetValue<int>("MaxMemory");
            if (maxmemory == 0) maxmemory = 500;
            logger.LogInformation("MitmProxyAppJob 任务开始......");

            try
            {
                await mitmProxyAppService.CheckMemory();
            }
            catch (System.Exception ex )
            {
               logger.LogError(ex,ex.Message);
            }
            

            logger.LogInformation("MitmProxyAppJob 任务结束");
        }





    }
}
